package cn.xz.zgsg.activity


import android.util.Log
import cn.xz.zgsg.R
import cn.xz.zgsg.base.BaseActivity
import cn.xz.zgsg.bean.CommonLib
import cn.xz.zgsg.bean.DeviceList
import cn.xz.zgsg.bean.U3dListBean
import cn.xz.zgsg.databinding.ActivitySecondBinding
import cn.xz.zgsg.net.ControlDevice
import cn.xz.zgsg.net.HttpEvent
import cn.xz.zgsg.util.GetListUtils
import cn.xz.zgsg.util.JsonUtil
import cn.xz.zgsg.util.ToastUtil
import cn.xz.zgsg.util.U3dHttp

class SecondActivity : BaseActivity<ActivitySecondBinding>(R.layout.activity_second) {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //默认一级特殊表示
    private var mTestFirst = 10086


    //获取数据通用回调，因为请求异步，加回调判断
    interface OnMListener {
        fun getData(list: ArrayList<GetListUtils.TestData>) //有参方法
    }
    override fun initData() {
        //获取跳转数据
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        //获取当前设备信息
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            mDataBind.tvTitle.text = mDevice!!.name
        }
        //如果是多级列表
        //设置层级
        setList()

    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
    }


    private fun setList() {
        //这个集合存储的是每一层级的样式以及信息，会根据该集合的长度去创建最大多少级
        val mList: ArrayList<GetListUtils.GetListData> = ArrayList<GetListUtils.GetListData>()
        mList.add(GetListUtils.GetListData(itemSize = 16f, leftMargin = 20))
        mList.add(GetListUtils.GetListData(itemSize = 15f))
        GetListUtils.find(mDataBind.clAll, this@SecondActivity, mList)

        //设置一级目录
        getListContext(mTestFirst,true,object :OnMListener{
            override fun getData(list: ArrayList<GetListUtils.TestData>) {
                list.forEach {
                    if (it.Tag == mTag){
                        it.select = true
                    }
                }
                GetListUtils.setListData(1, list, object : GetListUtils.OnListener {
                    override fun onClick(l: GetListUtils.TestData) {
                        mTag = l.Tag!!
                        mDevice = ControlDevice.getThisDevice(mTag)
                        setList2(l.Tag!!)
                    }
                })
            }

        })
    }

    //二级列表
    private fun setList2(tag: Int) {
        getListContext(tag,true,object :OnMListener{
            override fun getData(list: ArrayList<GetListUtils.TestData>) {
                if (list.size > 0) {
                    Log.e("TAG", "setListData--->: 这是栏目")
                } else {
                    Log.e("TAG", "setListData--->: 这是内容")
                }
                GetListUtils.setListData(2, list, object : GetListUtils.OnListener {
                    override fun onClick(l: GetListUtils.TestData) {

                    }
                })
            }
        })
    }

    //根据tag返回对应的集合
    private fun getListContext(tag: Int,isGetParent: Boolean,mListener: OnMListener){
        var mContext = ""
        if (tag == mTestFirst){
            mContext = HttpEvent.TITLE
            getLocalData(tag,mContext,mListener)
        }else{
            when (mTag) {
                //获取本地写死数据
                13186 -> {
                    mContext = HttpEvent.QS_DATA
                    getLocalData(tag,mContext,mListener)
                }
                //获取Unity数据
                13184 -> {
                    getUnitData(tag,isGetParent,mListener)
                }
                else -> {
                    mListener.getData(arrayListOf())
                }
            }
        }
    }

    //获取本地写死数据
    private fun getLocalData(tag: Int,mContext: String,mListener: OnMListener){
        val list: ArrayList<GetListUtils.TestData> = ArrayList<GetListUtils.TestData>()
        if (mContext != "") {
            for (d in JsonUtil.jsonToList(mContext, GetListUtils.TestData::class.java)!!) {
                if (d.ParentTag == tag || tag == mTestFirst) {
                    list.add(d)
                }
            }
        }
        mListener.getData(list)
    }

    //请求获取Unit数据
    private fun getUnitData(tag: Int, isGetParent: Boolean, mListener: OnMListener) {
        U3dHttp.getU3dData(this,mDevice!!.ip
        ) { mBean ->
            if (mBean != null) {
                setMList(tag, mBean, isGetParent, mListener)
            }else{
                mListener.getData(arrayListOf())
            }
        }
    }

    private fun setMList(tag: Int, response: U3dListBean, isGetParent: Boolean, mListener: OnMListener) {
        var mPTag = tag
        val bhList: List<U3dListBean.BhList> = response.list
        val subColumns: List<U3dListBean.SubColumns> = response.subColumns
        if (isGetParent) {
            //筛选出父类
            for (b in subColumns) {
                if (0 == b.parentTag && canGet(b.name)) {
                    mPTag = b.tag
                }
            }
        }

        //筛选出对应的二级数据
        val mSXList: ArrayList<GetListUtils.TestData> = ArrayList<GetListUtils.TestData>()
        for (b in subColumns) {
            if (mPTag == b.parentTag && canGet(b.name)) {
                mSXList.add(GetListUtils.TestData(b.name, b.tag, b.parentTag))
            }
        }

        if (mSXList.isEmpty()) { //无数据，从list里面筛选
            for (h in bhList) {
                if (h.tag == mPTag && canGet(h.title)) {
                    mSXList.add( GetListUtils.TestData(h.title, h.id, h.tag))
                }
            }
        }

        mListener.getData(mSXList)
        //如果此时总数据还为0,则将list全部添加进去
        /*if (mSXList.size() == 0) {
            if (subColumns.size() == 0) {
                for (BhListBean.BhList h : bhList) {
                    mSXList.add(setData(h.getTitle(), h.getTag(), h.getID()));
                }
            }
        }*/
    }


    //动态筛选不需要的字符串
    private fun canGet(s: String): Boolean {
        return s !== "null" && !s.contains(".png") && !s.contains(".jpg")
    }
}